from django import forms
from captcha.fields import CaptchaField

from user.models import CustomUser


class UserLoginForm(forms.Form):
    username = forms.CharField(label="用户名", widget=forms.TextInput(attrs={
        "class": "form-control",
        "placeholder": "请输入用户名"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
class UserRegisterForm(forms.ModelForm):
    password = forms.CharField(label='密码',widget=forms.PasswordInput(attrs={
        'class':"form-control",
        "placeholder":"请输入密码"
    }))
    password2 = forms.CharField(label='确认密码', widget=forms.PasswordInput(attrs={
        'class': "form-control",
        "placeholder": "请再次输入密码"
    }))
    captcha = CaptchaField(label='验证码', error_messages={'invalid': '验证码错误'})

    class Meta:
        model = CustomUser
        fields = ['email']
        widgets = {
            'email': forms.EmailInput(attrs={
                "class": "form-control",
                "placeholder": "请输入邮箱"
            }),

        }

    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2




class UserPassWordChangeForm(forms.Form):
    old_password = forms.CharField(label="原始密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="确认密码", widget=forms.PasswordInput(attrs={
        "class": "form-control",
        "placeholder": "请再次输入密码"
    }))


    def clean_password(self):
        password = self.cleaned_data.get('password')
        if len(password) < 6:
            raise forms.ValidationError("密码长度要大于6位")
        return password

    def clean_password2(self):
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        if password != password2:
            raise forms.ValidationError("密码不一致")
        return password2

class UserInfoChangeForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        fields = ['username', 'head']
        widgets = {
            'username': forms.TextInput(attrs={
                "class": "form-control",
                "placeholder": "请输入用户名"
            }),
            'head': forms.FileInput(attrs={})
        }
        labels = {
            'head': "头像"
        }

    def clean_head(self):
        head = self.cleaned_data.get('head')
        # 示例：头像大于2MB就报错
        if head and head.size > 2 * 1024 * 1024:
            raise forms.ValidationError("头像不能大于2MB")
        return head